package com.sun.alen.service.Impl;

import com.sun.alen.entity.Admin;
import com.sun.alen.mapper.AdminMapper;
import com.sun.alen.service.AdminService;
import com.sun.alen.utils.PhoneUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

    @Override
    public Admin login(Admin admin) {
        return adminMapper.login(admin);
    }

    @Override
    public boolean update(Admin admin) {
        return adminMapper.updateAdminPhoto(admin);
    }

    @Override
    public Admin selectById(int adminId) {
        return adminMapper.selectAdminById(adminId);
    }

    @Override
    public boolean updatePassword(Admin admin) {
        return adminMapper.updateAdminPassword(admin);
    }

    @Override
    public int send(String phone) {
        PhoneUtils phoneUtils=new PhoneUtils();
        if(phone.isEmpty()){
            return 1;
        }
        boolean flag= phoneUtils.sendCode(phone);
        if(flag){
            return 0;
        }else {
            return 2;
        }
    }

    @Override
    public int repass(String phone, String pass, String verify) {
        PhoneUtils phoneUtils=new PhoneUtils();
        boolean flag=phoneUtils.verifyCode(phone,verify);
        if(flag){
            boolean status=adminMapper.repass(phone,pass);
            if(status){
                return 0;
            }else {
                return 1;
            }
        }else {
            return 2;
        }
    }
}
